<template>
  <div class="comment-center">
    <div class="page-header">
      <h1 class="page-title">评论中心</h1>
    </div>

    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <div v-else-if="error" class="error-container">
      <p>{{ error }}</p>
      <button @click="fetchComments" class="retry-btn">重试</button>
    </div>

    <div v-else-if="orders.length === 0" class="empty-state">
      <i class="fas fa-comments"></i>
      <p>暂无评论</p>
    </div>

    <div v-else class="comment-list">
      <div v-for="order in orders" :key="order.id" class="comment-card">
        <div class="order-info">
          <div class="order-header">
            <span class="order-id">订单号：{{ order.orderNo }}</span>
            <span class="order-date">{{ order.createTime }}</span>
          </div>
          
          <div class="order-items">
            <div v-for="(item, index) in order.orderItems" :key="index" class="order-item">
              <div class="item-image">
                <img :src="item.productImage || 'https://via.placeholder.com/100'" :alt="item.productName">
              </div>
              <div class="item-info">
                <h3 class="item-name">{{ item.productName }}</h3>
                <div class="item-price-qty">
                  <span class="price">¥{{ formatPrice(item.price) }}</span>
                  <span class="quantity">x{{ item.quantity }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="comment-section">
          <div class="comment-header">
            <div class="comment-rating">
              <el-rate
                :model-value="order.comment.rating"
                disabled
                show-score
                text-color="#ff9900"
              />
            </div>
            <span class="comment-time">{{ order.comment.createTime }}</span>
          </div>
          <div class="comment-content">
            {{ order.comment.content }}
          </div>
          <div v-if="order.comment.images" class="comment-images">
            <el-image
              v-for="(image, index) in order.comment.images.split(',')"
              :key="index"
              :src="image"
              :preview-src-list="order.comment.images.split(',')"
              fit="cover"
              class="comment-image"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAllCommentedOrders } from '../../axios/order'

const orders = ref([])
const loading = ref(false)
const error = ref(null)

// 获取所有已评论订单
const fetchComments = async () => {
  loading.value = true
  error.value = null
  
  try {
    const response = await getAllCommentedOrders()
    if (Array.isArray(response)) {
      orders.value = response
    } else {
      error.value = '获取评论列表失败'
    }
  } catch (err) {
    console.error('获取评论列表失败:', err)
    error.value = '获取评论列表失败，请稍后重试'
  } finally {
    loading.value = false
  }
}

// 格式化价格
const formatPrice = (price) => {
  if (!price) return '0.00'
  return Number(price).toFixed(2)
}

onMounted(() => {
  fetchComments()
})
</script>

<style scoped>
.comment-center {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 60px);
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 28px;
  color: #2c3e50;
  margin-bottom: 20px;
  font-weight: 600;
}

.comment-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.comment-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.order-info {
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.order-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  color: #666;
}

.order-items {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.order-item {
  display: flex;
  gap: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.order-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.item-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.item-name {
  font-size: 16px;
  margin: 0;
  color: #2c3e50;
  font-weight: 500;
}

.item-price-qty {
  display: flex;
  justify-content: space-between;
  color: #666;
}

.comment-section {
  padding: 20px;
  background: #fafafa;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.comment-time {
  color: #999;
  font-size: 14px;
}

.comment-content {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
  white-space: pre-wrap;
}

.comment-images {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.comment-image {
  width: 100px;
  height: 100px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.3s;
}

.comment-image:hover {
  transform: scale(1.05);
}

.loading-container,
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #4CAF50;
  border-radius: 50%;
  margin-bottom: 20px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-container p {
  color: #f44336;
  margin-bottom: 20px;
}

.retry-btn {
  padding: 8px 15px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.empty-state i {
  font-size: 60px;
  color: #ddd;
  margin-bottom: 20px;
}

.empty-state p {
  color: #666;
  font-size: 18px;
}

@media (max-width: 768px) {
  .comment-center {
    padding: 15px;
  }
  
  .comment-images {
    gap: 8px;
  }

  .comment-image {
    width: 80px;
    height: 80px;
  }
}
</style> 